import { withPluginApi } from "discourse/lib/plugin-api";

export default {
  name: "hide-replies-to-guests",
  initialize() {
    withPluginApi((api) => {
      const currentUser = api.getCurrentUser();
      if (!currentUser) {
        api.onPageChange(() => {
          const hiddenPosts = Array.from(document.querySelectorAll('.post-hidden[data-post-number]'));
          hiddenPosts.forEach((el, idx) => {
            if (idx === 0) {
              el.innerHTML = `
                <div class="login-prompt" style="padding:2em;text-align:center;">
                  <p>请登录后查看回复内容。</p>
                  <a href="/login" class="btn btn-primary">登录</a>
                </div>
              `;
              el.style.display = "";
            } else {
              el.remove();
            }
          });
        });
      }
    });
  }
};
